﻿Date.prototype.pattern=function(fmt) {        
    var o = {        
    "M+" : this.getMonth()+1, //月份        
    "d+" : this.getDate(), //日        
    "h+" : this.getHours() == 0 ? 12 : this.getHours(), //小时        
    "H+" : this.getHours(), //小时        
    "m+" : this.getMinutes(), //分        
    "s+" : this.getSeconds(), //秒        
    "q+" : Math.floor((this.getMonth()+3)/3), //季度        
    "S" : this.getMilliseconds() //毫秒        
    };        
    var week = {        
    "0" : "\u65e5",        
    "1" : "\u4e00",        
    "2" : "\u4e8c",        
    "3" : "\u4e09",        
    "4" : "\u56db",        
    "5" : "\u4e94",        
    "6" : "\u516d"       
    };        
    if(/(y+)/.test(fmt)){        
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));        
    }        
    if(/(E+)/.test(fmt)){        
        fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "\u661f\u671f" : "\u5468") : "")+week[this.getDay()+""]);        
    }        
    for(var k in o){        
        if(new RegExp("("+ k +")").test(fmt)){        
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));        
        }        
    }        
    return fmt;        
}   



  Ext.define('workflow.models.statistics', {
  extend: 'Ext.data.Model',
  alias:'widget.statistics',
  fields: [
  { type:'string', name: 'dw' },
  { type:'string', name: 'rq' },
  { type:'string', name: 'hj_hjyhs' },
  { type:'string', name: 'hj_xqyhs' },
  { type:'string', name: 'hj_xql' },
  { type:'string', name: 'za_hjyhs' },
  { type:'string', name: 'za_xqyhs' },
  { type:'string', name: 'za_xql' },
  { type:'string', name: 'yb_hjyhs' },
  { type:'string', name: 'yb_xqyhs' },
  { type:'string', name: 'yb_xql' },
  { type:'string', name: 'zd_hjyhs' },
  { type:'string', name: 'zd_xqyhs' },
  { type:'string', name: 'zd_xql' }
  ]
  });
    Ext.define('workflow.models.org', {
  extend: 'Ext.data.Model',
  alias:'widget.org',
  fields: [
  { type:'string', name: 'id' },
  { type:'string', name: 'ParentID' },
  { type:'string', name: 'orderID' },
  { type:'string', name: 'isgroup' },
  { type:'string', name: 'dwCode' },
  { type:'string', name: 'dwName' },
  { type:'int', name: 'dwType' },
  { type:'int', name: 'dwOwner' },
  { type:'int', name: 'Remark' }
  ]
  });
  var statisticsStore=Ext.create('Ext.data.Store',{
                    model:'workflow.models.statistics',
                    //autoLoad: true,
                    autoSync: true,
                    proxy: {
                        type: 'ajax',
                        api: {
                        read: 'statistics_ajax.aspx?action=read'
                        },
                        reader: {
                            type: 'json',
                            successProperty: 'success',
                            root: 'data',
                            totalProperty:'total',
                            messageProperty: 'message'
                        },
                        extraParams: {
                            kssj: 100,
                            jssj:100,
                            dw:100
                            
                        },
                        listeners: {
                            exception: function(proxy, response, operation) {
                                Ext.MessageBox.show({
                                    title: 'REMOTE EXCEPTION',
                                    msg: operation.getError(),
                                    icon: Ext.MessageBox.ERROR,
                                    buttons: Ext.Msg.OK
                                });
                            }
                        },
                        callback:function(){
                        
                        }
                    }
                });


var orgStore=Ext.create('Ext.data.Store',{
                    model:'workflow.models.org',
                    autoLoad: true,
                    autoSync: true,
                    queryMode: 'local',
                    proxy: {
                        type: 'ajax',
                        url: '../workflow/workflow_org_ajax.aspx?action=read&type=org',
                        reader: {
                            type: 'json',
                            root: 'data'
                        }
                        }
                    
                });

Ext.define('statistics.option', {
    extend: 'Ext.panel.Panel',
    alias:'widget.option',
    height: 66,
    width: 762,
    layout: {
        type: 'column'
    },
    title: '查询条件',

    initComponent: function() {
        var me = this;

        Ext.applyIf(me, {
            items: [
                {
                    xtype: 'datefield',
                    margin: '5 0 0 10',
                    padding: '',
                    fieldLabel: '开始时间',
                    editable:false,
                    format: 'Y-m-d',
                    id:'kssj',
                    labelWidth: 60,
                    columnWidth: 0.28
                },
                {
                    xtype: 'datefield',
                    margin: '5 0 0 10',
                    fieldLabel: '结束时间',
                    format: 'Y-m-d',
                    editable:false,
                    id:'jssj',
                    labelWidth: 60,
                    columnWidth: 0.28
                },
                {
                    xtype: 'combobox',
                    margin: '5 0 0 10',
                    fieldLabel: '单位',
                    labelWidth: 30,
                    columnWidth: 0.34,
                    id:'dw',
                    displayField:'dwName',
                    valueField:'dwName',
                    store:orgStore
                },
                {
                    xtype: 'button',
                    margin: '5 0 0 10',
                    text: '统计',
                    columnWidth: 0.1,
                    handler:this.onStatistics
                }
            ]
        });

        me.callParent(arguments);
    }
});




Ext.define('workflow.views.tasklist', {
                extend: 'Ext.grid.Panel',
                alias: 'widget.tasklist',

                requires: [
        'Ext.grid.plugin.CellEditing',
        'Ext.form.field.Text',
        'Ext.toolbar.TextItem'//,
       // 'Ext.grid.CheckboxSelectionModel'
    ],
        columnLines: true,
      
                title:'统计结果',
                initComponent: function() {

                    this.editing = Ext.create('Ext.grid.plugin.CellEditing');
                
                    Ext.apply(this, {
                        iconCls: 'icon-grid',
                        //frame: true,
                      //  plugins: [this.editing],
                        dockedItems: [{
                                                     xtype: 'toolbar',
                                                    items: [{
                                                        iconCls: 'icon-grid',
                                                        text: '导出',
                                                        itemId: 'export',
                                                        id:'export',
                                                        scope: this,
                                                        handler: this.onExport
                                                    }]
                                                    }],
                                        columns: [Ext.create('Ext.grid.RowNumberer'),/*{
                                            text: 'id',
                                            width: 40,
                                            sortable: true,
                                            hidden:true,
                                            dataIndex: 'id'
                                        }, */{
                                            header: '单位',
                                            width: 200,
                                            sortable: true,
                                            dataIndex: 'dw',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }, {
                                            header: '日期',
                                            width: 200,
                                            sortable: true,
                                            dataIndex: 'rq',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }, {text:"合计",columns:[{
                                            header: '合计隐患数',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'hj_hjyhs',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }, {
                                            header: '消缺隐患数',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'hj_xqyhs',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }, {
                                            header: '消缺率',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'hj_xql',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }]}, {text:"障碍隐患",columns:[{
                                            header: '合计隐患数',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'za_hjyhs',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }, {
                                            header: '消缺隐患数',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'za_xqyhs',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }, {
                                            header: '消缺率',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'za_xql',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }]},{text:"一般隐患",columns:[{
                                            header: '合计隐患数',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'yb_hjyhs',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }, {
                                            header: '消缺隐患数',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'yb_xqyhs',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }, {
                                            header: '消缺率',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'yb_xql',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }]},{text:"重大隐患",columns:[{
                                            header: '合计隐患数',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'zd_hjyhs',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }, {
                                            header: '消缺隐患数',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'zd_xqyhs',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }, {
                                            header: '消缺率',
                                            width:75,
                                            sortable: true,
                                            dataIndex: 'zd_xql',
                                            field: {
                                                type: 'textfield'
                                            }
                                        }]}]
                                        });
                                        this.callParent();
                                        this.getSelectionModel().on('selectionchange', this.onSelectChange, this);
                                    },
                                    onSelectChange: function(selModel, selections) {
                                        //this.down('#viewcontent').setDisabled(selections.length === 0);
                                        //this.down('#viewrecord').setDisabled(selections.length === 0);
                                    }
                                });
                                
                                
                                
Ext.onReady(function(){
Ext.tip.QuickTipManager.init();

   Ext.create('Ext.Viewport', 
   {
        width: 960,
        height: 600,
        x: 0,
        y: 0,
        layout: {
            type: 'border',
            padding: 5
        },
        defaults: {
            split: true
        },
        items: [{
        region:'center',
        layout:'border',
        border:false,
        items:[{
            xtype:'option',
            title: '统计条件',
            id:'option1',
            itemId:'option1',
            region:'north',
            collapsible: true,
           // height:80,
            //width:750,
            onStatistics: function() {
                    if(Ext.getCmp('jssj').getValue()<Ext.getCmp('kssj').getValue()){
                        Ext.MessageBox.show({
                            title: '提示',
                            msg: '结束日期必须大于开始日期',
                            buttons: Ext.Msg.OK
                           });
                        }
                        else
                        {
                             statisticsStore.load({
                                 params: {
                                                dw: Ext.getCmp('dw').getValue()==null?"":Ext.getCmp('dw').getValue(),
                                                kssj: Ext.getCmp('kssj').getValue() ? Ext.getCmp('kssj').getValue().pattern('yyyy-MM-dd') : null,
                                                jssj: Ext.getCmp('jssj').getValue()?Ext.getCmp('jssj').getValue().pattern('yyyy-MM-dd'):null
                                         }
                            }); 
                        }
                        
                    }
        },{
            xtype:'tasklist',
            id:'tasklist1',
            region:'center',
            width:200,
            collapsible: true,
            
            height:200,
            store:statisticsStore,
            onExport:function(){
                var str='DownStatistics.aspx?kssj='
                str+=Ext.getCmp('kssj').getValue().pattern('yyyy-MM-dd')+'&jssj='+Ext.getCmp('jssj').getValue().pattern('yyyy-MM-dd')+'&dw='+(Ext.getCmp('dw').getValue()==null?"":Ext.getCmp('dw').getValue());
               openWindow(str,null);
            }
         }]
     }]
    });
 });
 
     function openWindow(str_url, dt_param) {
        var str_form_head = '<form name="tempForm" action="' + str_url + '" method="post" style="display:none">';
        var str_form_foot = '</form>';
        var str_form_param = '<textarea name="id">' + dt_param + '</textarea>';
        var str_javascript_execute = '<script type="text/javascript">document.tempForm.submit();</sc' + 'ript>';

        var arr_data = new Array();
        arr_data.push('<h3>请稍候 ...</h3>');
        arr_data.push(str_form_head);
        arr_data.push(str_form_param);
        arr_data.push(str_form_foot);
        arr_data.push(str_javascript_execute);
        var oWin = window.open('');
        oWin.document.write(arr_data.join(''));
        return oWin;
    } 